package vn.edu.saigontech.ncms.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import vn.edu.saigontech.ncms.model.system.ReceiptPercentDiscountItem;
import vn.edu.saigontech.ncms.service.ReceiptPercentDiscountItemService;

@SuppressWarnings("unchecked")
public class ReceiptPercentDiscountItemServiceImpl extends HibernateGenericService<ReceiptPercentDiscountItem, Long> implements ReceiptPercentDiscountItemService {
	
	@Override
	public Collection<ReceiptPercentDiscountItem> findAll() {
		Criteria criteria = this.createCriteria();
		criteria.addOrder(Order.asc("id"));
		return criteria.list();
	}
	
	@Override
	public Collection<ReceiptPercentDiscountItem> findAllAreActive() {
		Criteria criteria = this.createCriteria();
		criteria.add(Restrictions.eq("active", Boolean.TRUE));
		criteria.addOrder(Order.asc("id"));
		return criteria.list();
	}

	@Override
	public Map<String, List<ReceiptPercentDiscountItem>> findAllAndSeparate() {
		Criteria criteria = this.createCriteria();
		criteria.add(Restrictions.eq("active", Boolean.TRUE));
		criteria.addOrder(Order.asc("id"));
		List list = criteria.list();
		
		Map<String, List<ReceiptPercentDiscountItem>> ret = new HashMap<String, List<ReceiptPercentDiscountItem>>();
		for(int i = 0; i < list.size(); i++) {
			ReceiptPercentDiscountItem item = (ReceiptPercentDiscountItem) list.get(i);
			List<ReceiptPercentDiscountItem> values = ret.get(item.getValueDescription());
			if(values != null) {
				values.add(item);
			} else {
				List<ReceiptPercentDiscountItem> newValue = new ArrayList<ReceiptPercentDiscountItem>();
				newValue.add(item);
				ret.put(item.getValueDescription(), newValue);
			}
		}
		return ret;
	}
}